package com.bw.controller.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.entity.customer.PiCustomer;
import com.bw.entity.linkman.PiLinkman;
import com.bw.entity.technology.PiOrder;
import com.bw.req.LoginCustomer;
import com.bw.resp.ResponseResult;
import com.bw.service.customer.IPiCustomerService;
import com.bw.service.linkman.IPiLinkmanService;
import com.bw.service.technology.IPiOrderService;
import com.bw.utils.auth.JwtUtils;
import com.bw.utils.auth.RsaUtils;
import com.bw.utils.auth.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/customerLogin")
public class CustomerLogin {

    private final Logger logger = LoggerFactory.getLogger(CustomerLogin.class);

    @Autowired
    private IPiCustomerService customerService;

    @Autowired
    private IPiLinkmanService linkmanService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IPiOrderService orderService;

    @GetMapping("sendCode/{tel}")
    public ResponseResult sendCode(@PathVariable("tel") String tel){
        logger.info("开始发送验证码");
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            result = customerService.sendCode(tel);
        }catch (Exception e){
            e.toString();
            logger.error("验证码发送错误"+e.toString());
        }
        logger.info("验证码发送结束");
        return result;
    }

    @PostMapping("login")
    public ResponseResult login(@RequestBody LoginCustomer loginCustomer){
        ResponseResult result = ResponseResult.SUCCESS();
        logger.info("开始验证码登录");
        try {
            String yzm = redisTemplate.boundValueOps("Y"+loginCustomer.getTel()).get();
            if (null==yzm){
                result.setMessage("验证码过期");
                result.setSuccess(false);
                return result;
            }
            if (!yzm.equals(loginCustomer.getCode())){
                result.setMessage("验证码错误");
                result.setSuccess(false);
                return result;
            }
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("tel",loginCustomer.getTel());
            PiLinkman linkman = linkmanService.getOne(queryWrapper);
            PiCustomer customer = customerService.getById(linkman.getCustomerId());
            PrivateKey privateKey = RsaUtils.getPrivateKey("C:\\1711A\\keys\\rsa.pri");//获取私钥
            UserInfo userInfo = new UserInfo();
            userInfo.setId(customer.getId());
            userInfo.setUsername(customer.getName());
            String token = JwtUtils.generateToken(userInfo, privateKey, 30);
            HashMap map = new HashMap();
            map.put("user",customer);
            map.put("token",token);
            map.put("linkman",linkman);
            result.setResult(map);
            result.setSuccess(true);
        }catch (Exception e){
            logger.error("开始验证码失败");
        }
        logger.info("开始验证码登录结束");
        return result;
    }

    @PostMapping("getOrder/{userid}")
    public ResponseResult getOrder(@PathVariable("userid") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper;
            queryWrapper = new QueryWrapper();
            queryWrapper.eq("state",3);
            queryWrapper.eq("customerId",id);
            List list = orderService.list(queryWrapper);
            result.setResult(list);
            result.setMessage("查询成功!");
        }catch (Exception e){
            result.setMessage("查询失败!");
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("getOrderById/{userId}")
    public ResponseResult getOrderById(@PathVariable("userId") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (id!=null){
                queryWrapper.eq("customerId",id);
            }
            List list = orderService.list(queryWrapper);
            result.setResult(list);
            result.setMessage("查询成功!");
        }catch (Exception e){
            result.setMessage("查询失败!");
            result.setSuccess(false);
        }
        return result;
    }

    @PostMapping("getOrderByOId/{id}")
    public ResponseResult getOrderByOId(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            PiOrder order = orderService.getById(id);
            ArrayList list = new ArrayList();
            list.add(order);
            result.setResult(list);
            result.setMessage("查询成功!");
        }catch (Exception e){
            result.setMessage("查询失败!"+e.toString());
            result.setSuccess(false);
        }
        return result;
    }
}
